Package xunome.game.multiplayerg

Source Code of xunome.game.multiplayerg.ConnectionServer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package xunome.game.multiplayerg;

import java.io.InterruptedIOException;
import javax.microedition.io.StreamConnection;
import xunome.xUnoME;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
/**
*
* @author Sem iNick
*/
public abstract class ConnectionServer /*implements Runnable*/ {

    private StreamConnectionNotifier server = null;
    private MultiPlayerManager manager;
    private String name;
    private xUnoME mid;
    private Thread listenerT;
    public boolean listening = false;
    private boolean paused =  false;

    public ConnectionServer(xUnoME mid, String name) {

        this.mid = mid;
        this.name = name;
    }

    public final void setManager(MultiPlayerManager man) {

        this.manager = man;
    }

    public final void setLocalServer(StreamConnectionNotifier server) {

        this.server = server;
    }

    public final xUnoME getMidlet() {

        return mid;
    }

    public final String getName() {

        return name;
    }

    public final void startListening() {

        if (!listening) {

            //listener = new Thread(this);
            //listener.start();
            listening = true;
            listener();
        }
    }

    public final MultiPlayerManager getManager() {

        return manager;
    }

    public final void listener() {

        listenerT = new Thread() {

            public void run() {
                try {

                    DataListener listener = manager.getDataListener();
                    while (listening) {

                        if (!paused) {

                            StreamConnection get = server.acceptAndOpen();
                            //#ifdef DEBUG
                            System.out.println("Connected");
                            //#endif
                            listener.addConnetion(get);
                            if (listener.getNumberListener() == 3) {

                                pauseListening();
                            }
                        }
                    }
                } catch (Exception e) {

                    //#if DEBUG
                    e.printStackTrace();
                    //#endif
                    //Only show error if server wasn't closed
                    if (listening) {
                       
                        showError("Server couldn't be initialized");
                    }
                }
            }
        };
        listenerT.start();
    }

    public void stopListening() {

        if ((listening) && (server != null)) {

            try {

                listening = false;
                //listenerT.interrupt();
                server.close();
            } catch (Exception e) {

                //#if DEBUG
                System.out.println("Error close server");
                //#endif
            }
        }
    }

    public void showError(String msg) {

        Alert a = new Alert("Error", msg, null, AlertType.ERROR);
        a.setTimeout(3000);
        mid.getLCD().setCurrent(a, new xunome.MultiPlayerForm(mid));
    }

    public void resumeListening() {

        paused = false;
    }

    public void pauseListening() {

       paused = true;
    }
}
TOP

Related Classes of xunome.game.multiplayerg.ConnectionServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.